<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :props="{ children: 'children', label: 'label' }">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <!-- 逻辑与  一假则假   判断有无子级  删除按钮的显示与隐藏  -->
                        <!-- v-if = false: 不会进行渲染,true:渲染 -->
                        <el-dropdown-item
                          v-if="data.children && data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :visible.sync="showDialog"
        :title="isEdit ? '编辑部门' : '新增部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="resetForm"
      >
        <!--解决数据不更新的问题: v-if="showDialog" 关闭和打开弹层,销毁和创建组件 -->
        <addOrEdit
          v-if="showDialog"
          :id="curId"
          ref="addOrEdit"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { transDataToTree } from '@/utils'
import addOrEdit from './deptDialog'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      showDialog: false,
      list: [],
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 加载部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res)
        // 删除数组第一个数据
        res.data.depts.shift()
        this.list = transDataToTree(res.data.depts)
        this.originList = res.data.depts.map((item) => {
          return {
            name: item.name,
            id: item.id,
            pid: item.pid,
            code: item.code
          }
        })
      } catch (error) {
        console.log(error)
      }
    },

    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
    },
    // 删除部门
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 做删除
          await delDepartment(id)
          this.loadDepartments()
          this.showDialog = false
        })
        .catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // })
        })
    },

    hSuccess() {
      // 重新渲染
      this.loadDepartments()
      // 关闭弹层
      this.showDialog = false
    },
    resetForm() {
      this.$refs.addOrEdit.resetForm()
    }
  }
}
</script>
<style></style>
